// 122
package org.hwadee.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hwadee.entity.Comment;
import org.hwadee.entity.Post;
import org.hwadee.service.CategoryService;
import org.hwadee.service.CommentService;
import org.hwadee.service.PageOptionsService;
import org.hwadee.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PageOptionsService pageOptionsService;

    @RequestMapping(value = {"/", "/post", "index.do"})
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                        Model model) {
        PageHelper.startPage(page,pageSize);
        ///帖子列表
        List<Post> posts = postService.getAllPosts();
        PageInfo<Post> pageInfo = new PageInfo<>(posts);
        model.addAttribute("pageInfo", pageInfo);
        //热门帖子
        addHotPosts(model, 10);
        //最新帖子
        addRecentPosts(model, 10);
        //公告列表
        int noticeId = categoryService.getCategoryByCategoryName("公告").getCategoryId();
        List<Post> noticeList = postService.getPostsByCategoryId(noticeId);
        model.addAttribute("noticeList", noticeList);
        //提供分页支持
        model.addAttribute("pageUrlPrefix", "/post?page");
        //注入页面基础数据元素
        model.addAttribute("userOperations",pageOptionsService.getAllUserOperations());
        model.addAttribute("pageOptions", pageOptionsService.getPageOperation());
        model.addAttribute("categoryList", categoryService.getAllCategories());

        return "Home/index";
    }

    private void addRecentPosts(Model model, int num) {
        List<Post> recentPosts = postService.listRecentPost(10);
        model.addAttribute("recentPostList", recentPosts);
    }

    private void addHotPosts(Model model, int num) {
        List<Post> hotPosts = postService.listPostByViewCount(10);
        model.addAttribute("hotPostList", hotPosts);
    }

    @RequestMapping(value = "/category/{categoryId}")
    public String pageByCategory(@RequestParam(value = "page", defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                 @PathVariable("categoryId") int categoryId,
                                 Model model) {
        //帖子列表
        System.out.println(categoryId);
        PageHelper.startPage(page,pageSize);
        List<Post> posts = postService.listPostByCategoryId(categoryId);
        PageInfo<Post> pageInfo = new PageInfo<>(posts);
        model.addAttribute("pageInfo", pageInfo);
        //热门帖子
        addHotPosts(model, 10);
        //最新帖子
        addRecentPosts(model, 10);
        //提供分页支持
        model.addAttribute("pageUrlPrefix", "/category?page");
        //注入页面基础数据元素
        model.addAttribute("userOperations",pageOptionsService.getAllUserOperations());
        model.addAttribute("pageOptions", pageOptionsService.getPageOperation());
        model.addAttribute("categoryList", categoryService.getAllCategories());
        model.addAttribute("category",categoryService.getCategoryById(categoryId));
        return "Home/Page/pageByCategory";
    }

    @RequestMapping(value = "/search")
    public String search(@RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                         @RequestParam("keywords") String keywords,
                         Model model) {
        PageHelper.startPage(page,pageSize);
        ///文章列表
        List<Post> posts = postService.getPostsByKeywords(keywords);
        PageInfo<Post> pageInfo = new PageInfo<Post>(posts);
        model.addAttribute("pageInfo", pageInfo);
        //热门帖子
        addHotPosts(model, 10);
        //最新帖子
        addRecentPosts(model, 10);
        //提供分页支持
        model.addAttribute("pageUrlPrefix", "/search?keywords=" + keywords + "&page");
        //注入页面基础数据元素
        model.addAttribute("userOperations",pageOptionsService.getAllUserOperations());
        model.addAttribute("pageOptions", pageOptionsService.getPageOperation());
        model.addAttribute("categoryList", categoryService.getAllCategories());
        return "Home/Page/search";
    }

    @RequestMapping("/404")
    public String notFound(@RequestParam(required = false) String message, Model model) {
        model.addAttribute("pageOptions", pageOptionsService.getPageOperation());
        return "Home/Error/404";
    }

    @RequestMapping("/403")
    public String page403(@RequestParam(required = false) String message, Model model) {
        model.addAttribute("pageOptions", pageOptionsService.getPageOperation());
        return "Home/Error/403";
    }

    @RequestMapping("/500")
    public String serverError(@RequestParam(required = false) String message, Model model) {
        model.addAttribute("pageOptions", pageOptionsService.getPageOperation());
        return "Home/Error/500";
    }
}
